package crafex.engine.graphics;

import crafex.engine.tools.IntVertex2D;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class GraphicTools {
	
	
	public static Bitmap add(Bitmap firstlayer, Bitmap secondlayer){
		Bitmap data = Bitmap.createBitmap(secondlayer.getWidth(), secondlayer.getHeight(), secondlayer.getConfig());
		Canvas temp = new Canvas(data);
		
		temp.drawBitmap(secondlayer, 0, 0, null);
		temp.drawBitmap(firstlayer, 0, 0, null);
		
		return data;
	}
	
	/**
	 * compines images
	 * the first images in array being the bottom
	 * @param images
	 * @return
	 */
	public static Bitmap add(Bitmap[] images){
		Bitmap data = Bitmap.createBitmap(images[0].getWidth(), images[0].getHeight(), images[0].getConfig());
		Canvas temp = new Canvas(data);
		
		for(int i = 0; i < images.length; i++){
			temp.drawBitmap(images[i], 0, 0, null);
		}
		
		return data;
	}
	
	public static Bitmap makeTextButtonImage(String text, Bitmap Buttonimage, Bitmap letters, IntVertex2D grid, boolean scale){
		char[] newtext = text.toCharArray();
		Bitmap data = null;
		Bitmap textmap = Bitmap.createBitmap((letters.getWidth() / grid.getX()) * newtext.length, (letters.getHeight() / grid.getY()), letters.getConfig());
		Canvas temp = null;
		Canvas textcanvas = new Canvas(textmap);
		for(int i = 0; i < newtext.length; i++){
			int carx = 0;
			int cary = 0;
			
			for(char tester = 'a'; tester != newtext[i]; tester++){
				if(carx < grid.getX() - 1){
					carx++;
				}else{
					carx = 0;
					cary++;
				}
			}
			int srcx = (letters.getWidth() / grid.getX()) * carx;
			int srcy = (letters.getHeight() / grid.getY()) * cary;
			int dstx = (letters.getWidth() / grid.getX()) * i;
			textcanvas.drawBitmap(letters, new Rect(srcx, srcy, srcx + (letters.getWidth() / grid.getX()), srcy + (letters.getHeight() / grid.getY())),
					new Rect(dstx, 0, dstx + (letters.getWidth() / grid.getX()), (letters.getHeight() / grid.getY())), null);
		}
		
		if(scale){
			//TODO  make button scaling
		}else{
			data = Bitmap.createBitmap(Buttonimage.getWidth(), Buttonimage.getHeight(), Buttonimage.getConfig());
			temp = new Canvas(data);
			temp.drawBitmap(Buttonimage, 0, 0, null);
		}
		temp.drawBitmap(textmap, (int) ((data.getWidth() - textmap.getWidth())/2), (int) ((data.getHeight() - textmap.getHeight())/2), null);
				
		return data;
	}
}
